package com.hemaapp.yjnh;

import android.content.Context;
import android.content.pm.PackageManager;
import android.support.multidex.MultiDex;

import xtom.frame.XtomConfig;
import xtom.frame.util.XtomBaseUtil;
import xtom.frame.util.XtomLogger;
import xtom.frame.util.XtomSharedPreferencesUtil;
import xtom.frame.util.XtomToastUtil;

import com.hemaapp.BaseConfig;
import com.hemaapp.HemaConfig;
import com.hemaapp.yjnh.db.SysInfoDBHelper;
import com.hemaapp.yjnh.db.UserDBHelper;
import com.hemaapp.yjnh.bean.SysInitInfo;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.hm_FrameWork.HemaApplication;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.taobao.hotfix.HotFixManager;
import com.taobao.hotfix.PatchLoadStatusListener;
import com.taobao.hotfix.util.PatchStatusCode;
import com.umeng.socialize.PlatformConfig;

/**
 * 该项目自定义Application
 */
public class BaseApplication extends HemaApplication {
	private static final String TAG = BaseApplication.class.getSimpleName();
	private SysInitInfo sysInitInfo;// 系统初始化信息
	private User user;
	private static BaseApplication application;

	// 屏幕宽度
	private int width;
	private int ten;

	public static BaseApplication getInstance() {
		return application;
	}

	{
		PlatformConfig.setWeixin("wx6756e87d810f7fd2", "d4624c36b6795d1d99dcf0547af5443d");
	}

	@Override
	public void onCreate() {
		//阿里百川 start
		PackageManager pm = getPackageManager();//context为当前Activity上下文
		String appVersion = "";
		try {
			appVersion = XtomBaseUtil.getAppVersionName(this);
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		if (!appVersion.equals("")) {
			String appId = "72381-1";
			HotFixManager.getInstance().initialize(this, appVersion, appId, true, new PatchLoadStatusListener() {
				@Override
				public void onload(int mode, int code, String info, int handlePatchVersion) {
					// 补丁加载回调通知
					if (code == PatchStatusCode.CODE_SUCCESS_LOAD) {
						// TODO: 10/24/16 表明补丁加载成功
//						XtomToastUtil.showShortToast(BaseApplication.this, "补丁加载成功");
					} else if (code == PatchStatusCode.CODE_ERROR_NEEDRESTART) {
//						XtomToastUtil.showShortToast(BaseApplication.this, "新补丁生效需要重启app");
						// TODO: 10/24/16 表明新补丁生效需要重启. 业务方可自行实现逻辑, 提示用户或者强制重启, 可以监听应用进入后台事件, 然后应用自杀
					} else {
						// TODO: 10/25/16 其它信息
//						XtomToastUtil.showShortToast(BaseApplication.this, "其它信息 code="+code);
					}
				}
			});
			HotFixManager.getInstance().queryNewHotPatch();
		}
		//阿里百川 end
		application = this;
		XtomConfig.LOG = BaseConfig.DEBUG;
		HemaConfig.UMENG_ENABLE = BaseConfig.UMENG_ENABLE;
		XtomConfig.DIGITAL_CHECK = true;
		XtomConfig.DATAKEY = "Dh5f1WtKNPms2Zn";
		String iow = XtomSharedPreferencesUtil.get(this, "imageload_onlywifi");
		XtomConfig.IMAGELOAD_ONLYWIFI = "true".equals(iow);
		XtomLogger.i(TAG, "onCreate");
		super.onCreate();
		//创建默认的ImageLoader配置参数  
		ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
		//进行初始化操作
		ImageLoader.getInstance().init(configuration);

	}

	@Override
	public void onLowMemory() {
		XtomLogger.i(TAG, "onLowMemory");
		super.onLowMemory();
	}

	@Override
	public void onTerminate() {
		XtomLogger.i(TAG, "onTerminate");
		super.onTerminate();
	}

	/**
	 * @return 当前用户
	 */
	public User getUser() {
		if (user == null) {
			UserDBHelper helper = new UserDBHelper(this);
			String username = XtomSharedPreferencesUtil.get(this, "username");
			user = helper.select(username);
			helper.close();
		}
		return user;
	}

	/**
	 * 设置保存当前用户
	 * 
	 * @param user
	 *            当前用户
	 */
	public void setUser(User user) {
		this.user = user;
		UserDBHelper helper = new UserDBHelper(this);
		if (user != null) {
			helper.insertOrUpdate(user);
			helper.close();
		}else {
			helper.clear();
		}
	}

	/**
	 * @return 系统初始化信息
	 */
	public SysInitInfo getSysInitInfo() {
		if (sysInitInfo == null) {
			SysInfoDBHelper helper = new SysInfoDBHelper(this);
			sysInitInfo = helper.select();
			helper.close();
		}
		return sysInitInfo;
	}

	/**
	 * 设置保存系统初始化信息
	 * 
	 * @param sysInitInfo
	 *            系统初始化信息
	 */
	public void setSysInitInfo(SysInitInfo sysInitInfo) {
		this.sysInitInfo = sysInitInfo;
		if (sysInitInfo != null) {
			SysInfoDBHelper helper = new SysInfoDBHelper(this);
			helper.insertOrUpdate(sysInitInfo);
			helper.close();
		}
	}

	public int getWidth() {
		if (width == 0) {
			width = getResources().getDisplayMetrics().widthPixels;
		}
		return width;
	}
	
	/**获取十个相对像素，在当前手机上占的位置
	 * @return
	 */
	public int getTenDip() {
		if (ten == 0) {
			ten = BaseUtil.dip2px(this, 10);
		}
		return ten;
	}

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
